
//
// 110705 - AH - (Opera) Created.
// 110706 - AH - (Bybanen *) Completed.
// 110708 - AH - (Special) toString().
// 110710 - AH - Renamed turns property to turnCount to be consistent with
//               Motor property.
// 110716 - AH - (Bien *) getOntologyNamesInSequence().
// 110717a- AH - addOntology(String).
// 110717b- AH - (Bien) createDefaultConfig().
//

package org.soekrat.engine.motor.passingstrategies;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.soekrat.engine.forest.Forest;
import org.soekrat.engine.mine.Mine;
import org.soekrat.engine.motor.Motor;
import org.soekrat.engine.network.Network;
import org.soekrat.engine.ontology.OntologyDatastructure;

/**
 * <p>
 *   Configuration of 
 *   {@link Motor}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 * @deprecated Logic here is moved to SequenceWheelProcess. 
 */
@Deprecated 
public final class CopyOfMotorConfiguration 
{
  private int m_scanAndPaintCount=-1;
  
  private int m_turns=-1;
  
  private List<Class<?>> m_sequence=new ArrayList<Class<?>>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public CopyOfMotorConfiguration()
  {
    m_sequence.add(Forest.class);
  }
  
  /**
   * <p>
   *   Sets scan and paint steps configuration has.
   * </p>
   * @param n Count to set.
   * @return {@code this}.
   * @throws IllegalArgumentException If {@code n<1}.
   */
  public CopyOfMotorConfiguration setScanPaintCount(int n)
  {
    if (n<1)
    {
      throw new IllegalArgumentException("n<1 : "+n);
    }
    
    m_scanAndPaintCount=n;
    return this;
  }
  
  /**
   * <p>
   *   Gets scan and paint steps configuration has.
   * </p>
   * @return Count, default is {@code 1}.
   */
  public int getScanPaintCount(){ return m_scanAndPaintCount; }
  
  /**
   * <p>
   *   Sets the number of turns.
   * </p>
   * @param n Count to set-
   * @return {@code this}.
   * @throws IllegalArgumentException If {@code n<1}.
   */
  public CopyOfMotorConfiguration setTurnCount(int n)
  {
    if (n<1)
    {
      throw new IllegalArgumentException("n<1 : "+n);
    }
    
    m_turns=n;
    return this;
  }
  
  /**
   * <p>
   *   Gets the number of turns.
   * </p>
   * @return Count, default is {@code 1}.
   */
  public int getTurnCount(){ return m_turns==-1 ? 1 : m_turns; }
  
  /**
   * <p>
   *  Adds ontology to the sequence of ontologies the
   *  {@link Motor} should use.
   * </p>
   * @param clazz Class of ontology.
   * @return {@code this}.
   */
  public CopyOfMotorConfiguration addOntology(Class<?> clazz)
  {
    if (clazz==null)
    {
      throw new NullPointerException("clazz");
    }
    
    m_sequence.add(clazz);
    
    return this;
  }
  
  /**
   * <p>
   *   Adds to the ontology sequence.
   * </p>
   * @param name Simple class name on ontology yo add.
   * @return {@code this}.
   * @throws IllegalArgumentException If ontology not found.
   */
  public CopyOfMotorConfiguration addOntology(String name)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    m_sequence.add(OntologyDatastructure.getOntologyClass(name));
    return this;
  }
  
  /**
   * <p>
   *   Sets sequence of ontology classes.
   * </p>
   * @param sequence Sequence to set.
   * @return {@code this}.
   * @throws IllegalArgumentException If {@code sequence} is empty.
   */
  public CopyOfMotorConfiguration setSequence(List<Class<?>> sequence)
  {
    if (sequence==null)
    {
      throw new NullPointerException("sequence");
    }
    
    int n=sequence.size();
    if (n==0)
    {
      throw new IllegalArgumentException("sequence is empty");
    }
    
    m_sequence.clear();
    for (int i=0; i<n; i++)
    {
      Class<?> oc=sequence.get(i);
      if (oc==null)
      {
        throw new NullPointerException("sequence.get("+i+")");
      }
      m_sequence.add(oc);
    }
    
    return this;
  }
  
  /**
   * <p>
   *   Gets sequence of ontology classes.
   * </p>
   * @return Sequence of classes. Default is a sequence of a single element,
   *         the {@code Forest class}.
   */
  public List<Class<?>> getSequence()
  {
    return new ArrayList<Class<?>>(m_sequence); 
  }
  
  /**
   * <p>
   *   Gets names on
   *   {@link OntologyDatastructure ontologies} in sequence in sequence. 
   * </p>
   * @return Names.
   */
  public String[] getOntologyNamesInSequence()
  {
    int n=m_sequence.size();
    String[] retVal=new String[n];
    for (int i=0; i<n; i++)
    {
      Class<?> c=m_sequence.get(i);
      retVal[i]=c.getSimpleName();
    }
    return retVal;
  }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
 
    sb.append(getClass().getSimpleName()).append("(");
    sb.append("scan-and-paint-count=").append(m_scanAndPaintCount)
      .append(", turns=").append(m_turns).append(", sequence=")
      .append(m_sequence);
    sb.append(")");
    
    return sb.toString();
  }
  
  /**
   * <p>
   *   Gets the default configuration.
   * </p>
   * @return Default
   *         {@link CopyOfMotorConfiguration}.
   */
  public static CopyOfMotorConfiguration createDefaultConfig()
  {
    CopyOfMotorConfiguration retVal=new CopyOfMotorConfiguration();
    //retVal.setTurnCount(1);
    
    List<Class<?>> oclasses=new ArrayList<Class<?>>();
    oclasses.add(Mine.class);
    oclasses.add(Forest.class);
    oclasses.add(Network.class);
    Collections.shuffle(oclasses);
    
    retVal.m_sequence=oclasses;
    
    return retVal;
  }
  
}
